#include "memory.h"
#include <string>
#include <boost/format.hpp>
#include <iostream>

void AllocateCharArray(char*& pointer, int size_in_bytes) {
    if (pointer) {
        std::string message = str(boost::format("Your pointer already points to something. Initialize to zero or free its memory first. Failing instead of allocating %d bytes") % size_in_bytes);
        throw MemoryAllocationException(message, size_in_bytes);
    }
    
    pointer = new char[size_in_bytes];
    if (!pointer) {
        std::string message = str(boost::format("Your system ran out of memory. Couldn't allocate %d bytes") % size_in_bytes);
        throw MemoryAllocationException(message, size_in_bytes);
    }
}

void FreeMemory(char*& pointer) {
    if (!pointer) {
        throw MemoryDeallocationException("Your pointer doesn't point to anything. Failing instead of freeing");
    }
    
    delete[] pointer;
    pointer = 0;
}
